<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'stackoverflow-user.php');

function stackoverflow_save_answers($answers, $question_id)
{
    $reply_args = array(
        'post_type' => 'question_reply',
        'posts_per_page' => 1,
    );

    foreach ($answers as $answer) {
        $slug = 'stackoverflow-' . intval(substr($answer->id, strripos($answer->id, '/') + 1));
        $reply_args['name'] = $slug;
        $reply_query = new WP_Query($reply_args);
        if ($reply_query->have_posts()) {
            // TODO: update answer
        } else {
            $author_id = stackoverflow_get_user_id($answer->user);

            $reply_id = wp_insert_post(array(
                'post_content' => $answer->content,
                'post_title' => $answer->title,
                'post_name' => $slug,
                'post_status' => 'publish',
                'post_author' => $author_id,
                'post_type' => 'question_reply'
            ));
            update_post_meta($reply_id, 'reply_type', 'answer');
            update_post_meta($reply_id, 'item-d', $slug);
            update_post_meta($reply_id, 'reply_object', $question_id);
        }
    }
}

function stackoverflow_parse_answers($m)
{
    $answers = array();


    foreach ($m->entry as $i => $answer) {
        if ($i == 0) {
            // TODO: update question
        } else {
            $model = new stdClass();
            $model->user = $answer->author;
            $model->content = $answer->summary;
            $model->id = $answer->id;
            $model->title = $answer->title;
            $model->published = $answer->published;
            $model->updated = $answer->updated;
            $answers[] = $model;
        }
    }
    return $answers;
}

function stackoverflow_get_answers()
{
    $args = array(
        'user-agent' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; LENW8)',
        'httpversion' => '1.1',
        'timeout' => 120
    );
    $question_args = array(
        'post_type' => 'question',
        'post_status' => 'draft', // TODO: 定期同步原有的问题?
        'posts_per_page' => 1,
        'orderby' => 'ID',
        'order' => 'ASC',
        'meta_key' => 'source',
        'meta_value' => 'stackoverflow',
    );
    $question_query = new WP_Query($question_args);

    foreach ($question_query->posts as $question) {
        var_dump($question);
        $item_id = get_post_meta($question->ID, 'item-id', true);
        if ($item_id) {
            $url = 'https://stackoverflow.com/feeds/question/' . $item_id;
            $content = wp_remote_get($url, $args);
            $xml = simplexml_load_string($content['body']);
            $model = json_decode(json_encode($xml));
            $answers = stackoverflow_parse_answers($model);
            var_dump($answers);
            if($answers&&count($answers)>0) {
                stackoverflow_save_answers($answers, $question->ID);   
            }
            wp_publish_post($question->ID);
        }
    }
}
